<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <p>我不需要变色</p>
    </ul>
    <script>
        const ul = document.querySelector("ul")
        ul.addEventListener("click", function (e) {
            // 只有点击的是li时才变颜色
            // 判断点击的元素的tagName
            if (e.target.tagName === "LI" ||e.target.nodeName==="LI") {
                e.target.style.color = "red"
            }else{
                alert("点击的不是li")
            }
        })
    </script>
</body>

</html>